<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop" ref="buttondiv">
          <table>
              <tr>
                  <td>快速查找</td>
                  <td>
                      <el-input style=" width:200px" v-model="searchtext"></el-input>
                  </td>
                  <td>检验组</td>
                  <td>
                      <rm-ddl-common :appendtobody="true" datatype="labgroup" v-model="labgroup" @change="labgroupchange" width="200px"></rm-ddl-common>
                  </td>
                  <td :width="buttonwidth"></td>
                  <td style="align:right">
                      <el-button @click="findList(), (isFirstLoading = false), (NewArr = []), (UpdateArr = [])" icon="el-icon-search" type="primary" v-has="'PINQUERY'">查询</el-button>
                  </td>
                  <td style="align:right">
                      <el-button @click="OpenDialog(1)" icon="el-icon-plus" v-has="'PADD'">新增</el-button>
                  </td>
                  <td style="align:right">
                      <el-button @click="OpenDialog(2)" icon="el-icon-edit" v-has="'PMODIFY'">修改</el-button>
                  </td>
                  <td style="align:right">
                      <el-button @click="OpenDialog(3)" icon="el-icon-delete" v-has="'PDELETE'">删除</el-button>
                  </td>
                  <td style="align:right">
                      <el-button @click="ExportCsv()" icon="el-icon-files">导出</el-button>
                      <!--v-has="'PEXPORT'"-->
                  </td>
                  <td>
                      <el-button @click="ImportCsv()" icon="el-icon-files">导入</el-button>
                      <!--v-has="'PEXPORT'"-->
                  </td>
              </tr>
          </table>
      </div>
      <div class="pagecenter">
          <rm-layout-right splitcolor="#ffffff" :splitwidth="3" :fixsize="480" :minfixsize="480" ref="layoutright">
              <div style="height:100%">
                  <vxe-table :data="list" :loading="loading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :row-class-name="rowClassName" :select-config="selectConfig" :sort-config="{ trigger: 'cell' }" @current-change="currentChangeEvent" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xTable" tableid="lis112maintb" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                      <vxe-table-column field="hospitalname" title="所属医院" width="100"></vxe-table-column>
                      <vxe-table-column field="itemclassname" title="所属项目类别" width="100"></vxe-table-column>
                      <vxe-table-column field="mdlname" title="模板名称" width="260"></vxe-table-column>
                      <vxe-table-column field="specimen_name" title="标本类型" width="80" align="center"></vxe-table-column>
                      <vxe-table-column field="inputcode" title="输入码" width="100"></vxe-table-column>
                      <vxe-table-column field="description" title="描述" width="200"></vxe-table-column>
                      <vxe-table-column field="txtinfo1" title="默认文字描述1" width="200"></vxe-table-column>
                      <vxe-table-column field="txtinfo2" title="默认文字描述2" width="200"></vxe-table-column>
                      <vxe-table-column field="lab_advice" title="默认实验室意见" width="200"></vxe-table-column>
                      <vxe-table-column field="invalid" title="停用" width="40" align="center">
                          <template v-slot="{ row }">
                              <el-checkbox false-label="" true-label="停用" v-model="row.invalid" disabled></el-checkbox>
                          </template>
                      </vxe-table-column>
                      <vxe-table-column field="vsrptunitcnt" title="对照报告单元数" width="100" align="right" header-align="center"></vxe-table-column>
                      <vxe-table-column field="inputmdlid" title="ID" width="80" align="right" header-align="center"></vxe-table-column>
                      <vxe-table-column field="labgroupname" title="工作组" width="100" align="center"></vxe-table-column>
                      <!-- <vxe-table-column :field="items.field" :key="items.field" :title="items.title" :width="items.width" sortable v-for="items in tableColumn"></vxe-table-column> -->
                  </vxe-table>
              </div>
              <template v-slot:fix>
                  <el-tabs @tab-click="handleTabClick" style="height:100%;" type="border-card">
                      <el-tab-pane label="基本信息">
                          <el-form :inline="true" :model="inputMdlright" @submit.native.prevent label-width="auto" ref="form">
                              <table height="100%" width="100%" style="border-spacing:5px 10px;" class="tablelayout">
                                  <tr>
                                      <td>模板名称：</td>
                                      <td>
                                          <el-input v-model="inputMdlright.mdlname" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>是否停用：</td>
                                      <td>
                                          <el-checkbox label="" v-model="invalidflag" disabled></el-checkbox>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>所属医院：</td>
                                      <td>
                                          <el-input v-model="inputMdlright.hospitalname" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>检验组：</td>
                                      <td>
                                          <el-input v-model="inputMdlright.labgroupname" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>输入码：</td>
                                      <td>
                                          <el-input v-model="inputMdlright.inputcode" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>所属项目类别：</td>
                                      <td>
                                          <el-input v-model="inputMdlright.itemclassname" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="vertical-align:top">描述：</td>
                                      <td>
                                          <el-input type="textarea" :rows="2" v-model="inputMdlright.description" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="vertical-align:top">默认文字描述1：</td>
                                      <td>
                                          <el-input type="textarea" :rows="2" v-model="inputMdlright.txtinfo1" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="vertical-align:top">默认文字描述2：</td>
                                      <td>
                                          <el-input type="textarea" :rows="2" v-model="inputMdlright.txtinfo2" readonly></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="vertical-align:top">默认实验意见：</td>
                                      <td>
                                          <el-input type="textarea" :rows="2" v-model="inputMdlright.lab_advice" readonly></el-input>
                                      </td>
                                  </tr>
                              </table>
                          </el-form>
                      </el-tab-pane>
                      <el-tab-pane label="包含项目">
                          <div ref="xTableDiv1" style="height:100%">
                              <vxe-table :data.sync="tableDataContain" :select-config="selectConfig" height="auto" :sort-config="{ trigger: 'cell' }" :loading="loadingContain" :row-class-name="CotainClassName" highlight-current-row highlight-hover-row border fit highlight-hover-column resizable header-align="center" show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                                  <vxe-table-column :field="items.field" :key="items.field" :title="items.title" :width="items.width" sortable v-for="items in tableColumnContain"></vxe-table-column>
                              </vxe-table>
                          </div>
                      </el-tab-pane>
                      <el-tab-pane @tab-click="ShowUnit()" label="报告单元使用情况">
                          <div ref="xTableDiv2" style="height:100%">
                              <vxe-table :data.sync="tableDataUnit" :select-config="selectConfig" height="auto" highlight-current-row :loading="loadingUnit" highlight-hover-row highlight-hover-column resizable show-header-overflow="tooltip" border fit show-overflow="tooltip" header-align="center" size="mini" stripe width="800px">
                                  <vxe-table-column :field="items.field" :key="items.field" :title="items.title" :width="items.width" sortable v-for="items in tableColumnUnit"></vxe-table-column>
                              </vxe-table>
                          </div>
                      </el-tab-pane>
                  </el-tabs>
              </template>
          </rm-layout-right>
      </div>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" :title="dialogtitle" :visible.sync="dialogFormVisible" @close="CancelTips()" v-if="dialogif" width="650px" style="padding:5px 5px 5px 5px" top="6vh">
          <div v-if="actionflag < 3">
              <el-form :inline="true" :model="inputMdl" @submit.native.prevent label-width="auto" ref="form">
                  <table style="padding:5px;width:100%;border:1px solid #ccc;border-bottom:0">
                      <tr>
                          <td style="color:red;">模板名称</td>
                          <td colspan="3">
                              <el-input style="width:100%;" v-model="inputMdl.mdlname" @input="mdlnameChange" @keyup.enter.native="autofocusEvent($event, 'mdlname')" ref="inputmdlname"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td>标本类型</td>
                          <td>
                              <rm-ddl-common :appendtobody="true" datatype="specimen" v-model="inputMdl.specimen_code" @change="specChange" @keyup.enter.native="autofocusEvent($event, 'spec')" width="100%" ref="inputspec"></rm-ddl-common>
                          </td>
                          <td>
                          <span v-if="isAreaMode" style="color:red;">所属医院</span>
                          <span v-if="!isAreaMode">所属医院</span>
                          </td>
                          <td>
                              <rm-ddl-common :appendtobody="true" datatype="hospital" v-model="inputMdl.hospcode" width="100%" @keyup.enter.native="autofocusEvent($event, 'hosp')" @change="hospChange" :disabled="isAreaMode && $tool.getSession($tool.loginutype) != 'G'" ref="inputhosp"></rm-ddl-common>
                          </td>
                      </tr>
                      <tr>
                          <td>输入码</td>
                          <td>
                              <el-input v-model="inputMdl.inputcode" @keyup.enter.native="autofocusEvent($event, 'incode')" ref="inputincode"></el-input>
                          </td>
                          <td></td>
                          <td style="text-align:left;margin-left:20px">
                              <el-checkbox label="本模板已经停用" style="text-align:left" v-model="inputMdl.invalid"></el-checkbox>
                          </td>
                      </tr>
                      <tr>
                          <td>所属项目类别</td>
                          <td>
                              <rm-ddl-rptitemclass :appendtobody="true" v-model="inputMdl.itemclasscode" @change="itemcalsschange" filterable @keyup.enter.native="autofocusEvent($event, 'itemclass')" ref="inputitemclass" width="100%"></rm-ddl-rptitemclass>
                          </td>
                          <td>检验组</td>
                          <td>
                              <rm-ddl-common :appendtobody="true" datatype="labgroup" v-model="inputMdl.labgroupid" @keyup.enter.native="autofocusEvent($event, 'labgroup')" @change="dialabgroupchange" ref="inputlabgroup" width="100%"></rm-ddl-common>
                          </td>
                      </tr>
                      <tr>
                          <td style="vertical-align:top">描述</td>
                          <td colspan="3">
                              <el-input :rows="2" type="textarea" v-model="inputMdl.description" ref="inputdesc"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td style="vertical-align:top">默认文字描述1</td>
                          <td colspan="3">
                              <el-input :rows="2" type="textarea" v-model="inputMdl.txtinfo1"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td style="vertical-align:top">默认文字描述2</td>
                          <td colspan="3">
                              <el-input :rows="2" type="textarea" v-model="inputMdl.txtinfo2"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td style="vertical-align:top">默认实验意见</td>
                          <td colspan="3">
                              <el-input :rows="2" type="textarea" v-model="inputMdl.lab_advice"></el-input>
                          </td>
                      </tr>
                  </table>

                  <div ref="xTableDiv3">
                      <vxe-table :data.sync="tableDataContainEdit" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }" :sort-config="{ trigger: 'cell' }" :mouse-config="{ selected: true }" :select-config="selectConfig" :keyboard-config="keyConfig" @cell-dblclick="DblEvent" @edit-closed="containeditclose" height="350px" highlight-current-row highlight-hover-row ref="xTable3" border resizable highlight-hover-column show-header-overflow="tooltip" show-overflow="tooltip" header-align="center" :loading="loadingContainEdit" size="mini" stripe width="980px">
                          <vxe-table-column field="rpt_itemname" key="rpt_itemname" title="报告项目" width="230px" sortable>
                              <template v-slot="{ row, rowIndex }">
                                  <!-- <rm-ddl-rptitem prgno="LIS112" :appendtobody="true" :isAllRpt="'1'" v-model="row.rpt_itemcode" @change="RptChangeEvent($event,row,rowIndex)" @keyup.enter.native="autofocusEvent($event,'rptitem',row)" @click.native="rptfocus" :itemclass="nowitemclass" width="220px"></rm-ddl-rptitem> -->

                                  <el-input @keyup.space.native="(dialogRptVisible = true), (tempRptItem = row)" @change="rptitemInput(row)" v-model="row.rpt_itemname" style="width:100%;height:18px" :autofocus="true" ref="rptitemname"></el-input>
                                  <!--<el-input style="220px" @keyup.space.native="dialogTableVisible3=true" readonly placeholder="请选择报告项目" ref="rptitem" v-model="row.rpt_itemcode">
                                      <i @click="RptChooseEvent(row)" class="el-input__icon el-icon-menu" slot="suffix"></i>
                                  </el-input>-->
                                  <!--<el-input width="220px" v-model="row.rpt_itemcode" @change="RptChangeEvent($event,row,rowIndex)" ></el-input>-->
                              </template>
                          </vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="defaultval" key="defaultval" title="默认值" width="80px" sortable>
                              <template v-slot:edit="{ row }">
                                  <el-autocomplete :popper-append-to-body="true" popper-class="mini-select" v-model="row.defaultval" :fetch-suggestions="querySearchtable" placeholder="默认值" style="width:100%;height:14px;">
                                      <template slot-scope="{ item }">
                                          <div class="name">{{ item.value }}</div>
                                      </template>
                                  </el-autocomplete>
                              </template>
                              <template v-slot="{ row }">
                                  <el-autocomplete :popper-append-to-body="true" popper-class="mini-select" v-model="row.defaultval" :fetch-suggestions="querySearchtable" placeholder="默认值" style="width:100%;height:14px;">
                                      <template slot-scope="{ item }">
                                          <div class="name">{{ item.value }}</div>
                                      </template>
                                  </el-autocomplete>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="seq" key="seq" title="序号" width="80px" sortable align="center"></vxe-table-column>
                          <vxe-table-column field="invalid" key="invalid" title="停用" width="50px" sortable align="center">
                              <template v-slot="{ row, rowIndex }">
                                  <el-checkbox v-model="row.invalid" class="incheckbox"></el-checkbox>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="defaultresult1" key="defaultresult1" title="结果1默认值" width="120px" sortable></vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="defaultresult2" key="defaultresult2" title="结果2默认值" sortable width="120px"></vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="defaultresult3" key="defaultresult3" title="结果3默认值" width="120px" sortable></vxe-table-column>
                          <vxe-table-column :edit-render="{ name: 'input', autoselect: true }" field="defaultresult_txt" key="defaultresult_txt" title="文字结果默认值" width="120px" sortable></vxe-table-column>
                      </vxe-table>
                  </div>
              </el-form>
              <div class="dialog-footer" slot="footer" style="text-align:right">
                  <div style="margin-top: 3px">
                      <el-button @click="AddRecord()" icon="el-icon-plus">新增明细</el-button>
                      <el-button @click="DelRecord()" icon="el-icon-delete">删除明细</el-button>
                      <el-button @click="CommitTips()">确 定</el-button>
                      <el-button @click="CancelTips()">取 消</el-button>
                  </div>
                  <!-- <table style="margin-left:381px">
                      <tr>
                          <td>
                              <el-button @click="AddRecord()" icon="el-icon-plus">新增明细</el-button>
                          </td>
                          <td>
                              <el-button @click="DelRecord()" icon="el-icon-delete">删除明细</el-button>
                          </td>
                          <td>
                              <el-button @click="CommitTips()">确 定</el-button>
                          </td>
                          <td>
                              <el-button @click="CancelTips()">取 消</el-button>
                          </td>
                      </tr>
                  </table> -->
              </div>
          </div>
          <div style="margin-left:260px" v-else>
              <span class="dialog-footer" slot="footer">
                  <el-button @click="CommitTips()">确 定</el-button>
                  <el-button @click="CancelTips()">取 消</el-button>
              </span>
          </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="提示" :visible.sync="dialogVisible" width="30%">
          <div class="el-message-box__status el-icon-warning"></div>
          <div class="el-message-box__message" style="padding-top:24px">
              <p>
                  确定要删除批输入模板记录<b style="color:red">{{ inputMdlright.mdlname }}</b>吗?
              </p>
          </div>
          <span slot="footer" class="dialog-footer">
              <el-button @click="CommitTips()">确 定</el-button>
              <el-button @click="CancelTips()">取 消</el-button>
          </span>
      </el-dialog>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialogRptVisible" :destroy-on-close="true" title="报告项目" v-if="dialogRptVisible" @close="$refs.xTable3.setActiveCell($refs.xTable3.getCurrentRow(), 'rpt_itemname')">
          <rm-chkboxSelect2 prgno="LIS112" @cancel="
        (dialogRptVisible = false),
          $refs.xTable3.setActiveCell($refs.xTable3.getCurrentRow(), 'rpt_itemname')
      " :isAllRpt="1" :rptunitdisabled="false" @selitem="addrptitem" @selitems="SelectRptItems" ref="rptdialog"></rm-chkboxSelect2>
      </el-dialog>
      <!--<el-dialog width="1000px" top="10px" :append-to-body="true" :close-on-click-modal="false" title="选择报告项目" :visible.sync="dialogTableVisible3" :destroy-on-close="true">
          <rm-pop-rptitem @cancel="dialogTableVisible3=false;" :isAllRpt="1" @selitem="addrptitem"></rm-pop-rptitem>
      </el-dialog>-->
  </div>
</template>

<script>
import XEUtils from "xe-utils";
export default {
  name: "LIS112",
  data() {
      return {
          loading: false,
          loadingContain: false,
          loadingContainEdit: false,
          loadingUnit: false,
          loadingRpt: false,
          buttonwidth: "",
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true
          },
          keyLocalConfig: {
              isArrow: true
          },
          inputMdl: {},
          tempinutMdl: {},
          showtempMdl: {},
          tempRptItem: {},
          tempRowIndex: 0,
          selectConfig: { tigger: "row" },
          searchtext: "",
          actionflag: 0,
          dialogFormVisible: false,
          dialogVisible: false,
          dialogif: true,
          labgroup: "",
          nowitemclass: "",
          DelIndex: -1,
          seqChangeflag: false,
          commitflag: false,
          invalidflag: false,
          Delmdlname: "",
          dialogTableVisible3: false,
          rptfocusflag: false,
          dialogRptVisible: false,
          form: {
              file: ""
          },
          limitNum: 1,
          tableData: [],
          tableTempData: [],
          tableDataContain: [],
          tableDataContainEdit: [],
          tableColumnContainEdit: [],
          tableDataUnit: [],
          tableColumn: [],
          tableColumnContain: [],
          tableColumnUnit: [],
          dialogtitle: "",
          CotainAddNum: 0,
          isFirstLoading: true,
          findflag: true,
          AddArray: 0,
          inputMdlright: {},
          curtabindex: 0,
          selectcontain: {},
          isAreaMode: true,
          UpdateArr: [],
          NewArr: [],
          defaultvalList: [],
          Options: {}
      };
  },
  computed: {
      list() {
          let filterName = this.searchtext.trim();
          if (filterName) {
              let filterRE = new RegExp(filterName, "gi");
              let num = this.AddArray;
              let rest = this.tableData.filter(item => {
                  return item.mdlname.toUpperCase().indexOf(filterName.toUpperCase()) > -1 || (item.inputcode ? item.inputcode.toUpperCase().indexOf(filterName.toUpperCase()) > -1 : false) || item.inputmdlid == num;
              });
              return rest;
          } else {
              return this.tableData;
          }
      }
  },
  watch: {
      searchtext(val) {
          var length = this.tableData.length;
          if (this.AddArray > 0) {
              this.AddArray = 0;
          }
          this.$nextTick(() => {
              var row = this.$refs.xTable.getCurrentRow();
              if (this.$refs.xTable.tableData.length > 0) row = this.$refs.xTable.tableData[0];
              if (row.inputmdlid) {
                  this.$refs.xTable.setCurrentRow(row);
                  this.inputMdlright = row;
                  this.Delmdlname = row.mdlname;
                  this.invalidflag = this.inputMdlright.invalid.length > 0;
                  this.tempRowIndex = this.$refs.xTable.getRowIndex(row);
                  this.ShowContain();
                  this.ShowUnit();
              }
          });
      }
  },
  created() {
      this.findList();
      this.getdefaultvalList();
  },
  mounted() {},
  updated() {
      this.getButtonWidth();
      if (this.isFirstLoading) {
          var selectrow = {};
          if (this.AddArray > 0) {
              selectrow = this.$refs.xTable.tableData.filter(item => {
                  return item.inputmdlid == this.AddArray;
              })[0];
          } else {
              if (this.tempRowIndex > 0) selectrow = this.$refs.xTable.getData(this.tempRowIndex);
              else selectrow = this.$refs.xTable.getData(0);
          }
          if (selectrow) {
              this.isFirstLoading = false;
              this.$nextTick(() => {
                  this.$refs.xTable.setCurrentRow(selectrow);
                  this.inputMdlright = selectrow;
                  this.Delmdlname = selectrow.mdlname;
                  this.invalidflagright = this.inputMdlright.invalid.length > 0;
                  this.$refs.xTable.scrollToRow(selectrow);
              });
          }
      }
  },
  methods: {
      querySearchtable(queryString, cb) {
          var res = this.defaultvalList;
          var code = this.$refs.xTable3.getCurrentRow().rpt_itemcode;
          res = res.filter(item => item.rpt_itemcode == code);
          var results = queryString ? res.filter(item => item.value.toLowerCase().indexOf(queryString.toLowerCase() > -1)) : res;
          cb(results);
      },
      getdefaultvalList() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "getdefaultvalList"
                  }
              })
              .then(res => {
                  this.defaultvalList = res;
              })
              .catch(err => {});
      },
      //双击弹窗添加项目
      addrptitem(c, n, row) {
          var currentrow = this.$refs.xTable3.getCurrentRow();
          var rowindex = this.$refs.xTable3.getRowIndex(currentrow);
          var hasrow = this.$refs.xTable3.tableFullData.filter(item => item.rpt_itemcode == c);
          if (hasrow.length > 0 && currentrow.rpt_itemcode != c) {
              this.$confirm("不能选择重复的项目", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  focusCancelButton: false,
                  type: "warning"
              })
                  .then(() => {
                      this.tempRptItem.rpt_itemname = "";
                      this.$refs.xTable3.setActiveCell(currentrow, "rpt_itemname").then(() => {
                          if (this.$refs.rptitemname) {
                              this.$refs.rptitemname.focus();
                          }
                      });
                  })
                  .catch(() => {
                      this.tempRptItem.rpt_itemname = "";
                      this.$refs.xTable3.setActiveCell(currentrow, "rpt_itemname").then(() => {
                          if (this.$refs.rptitemname) {
                              this.$refs.rptitemname.focus();
                          }
                      });
                  });
          } else {
              this.tempRptItem.rpt_itemcode = c;
              this.tempRptItem.rpt_itemname = n;
              if (this.dialogRptVisible) {
                  this.$nextTick(() => {
                      this.$refs.xTable3.setActiveCell(currentrow, "defaultval");
                  });
              }
          }
          this.dialogRptVisible = false;
      },
      SelectRptItems(rows) {
          var indexdata = this.$refs.xTable3.tableFullData.filter(item => item.rpt_itemcode == "" || item.rpt_itemcode == null);
          var indexs = [];
          for (let j = 0; j < indexdata.length; j++) {
              indexs.push(this.$refs.xTable3.tableFullData.indexOf(indexdata[j]));
          }
          for (let i = 0; i < rows.length; i++) {
              this.CotainAddNum = this.CotainAddNum + 1;
              let row = rows[i];
              var hasrow = this.$refs.xTable3.tableFullData.filter(item => item.rpt_itemcode == row.rpt_itemcode);
              if (hasrow.length > 0) {
                  continue;
              }
              var insertrow = {
                  rpt_itemname: row.rpt_itemname,
                  rpt_itemcode: row.rpt_itemcode,
                  defaultval: "",
                  seq: this.CotainAddNum * 10,
                  keyitem: row.keyitem
              };
              if (i < indexs.length) {
                  for (var item in insertrow) {
                      this.$refs.xTable3.tableFullData[indexs[i]][item] = insertrow[item];
                  }
              } else {
                  this.$refs.xTable3.insertAt(insertrow, -1);
              }
          }
          var currentrow = this.$refs.xTable3.getCurrentRow();
          if (this.dialogRptVisible) {
              this.$nextTick(() => {
                  this.$refs.xTable3.setActiveCell(currentrow, "defaultval");
              });
          }
          this.dialogRptVisible = false;
      },
      async rptitemInput(row) {
          // this.ddlData = await this.$ddldata.getrptunititem(this.myrptunitid);
          // var select = this.ddlData.filter(
          // 	(item) =>
          // 		item.rpt_itemcode.toUpperCase() == row.rpt_itemname.toUpperCase() ||
          // 		item.itemcode_en.toUpperCase() == row.rpt_itemname.toUpperCase() ||
          // 		item.rpt_itemname.toUpperCase() == row.rpt_itemname.toUpperCase() ||
          // 		item.inputcode.toUpperCase() == row.rpt_itemname.toUpperCase()
          // );
          if (select && select.length > 0) {
              this.tempRptItem = row;
              this.addrptitem(select[0].rpt_itemcode, select[0].rpt_itemname, select[0]);
          } else {
              row.rpt_itemname = "";
          }
      },
      DblEvent({ row, rowIndex, $rowIndex, column }) {
          switch (column.property) {
              case "rpt_itemname":
                  this.dialogRptVisible = true;
                  this.$nextTick(() => {
                      this.$refs.rptdialog.$refs.qcodeinput.focus();
                  });
                  this.tempRptItem = row;
                  break;
          }
      },
      currentChangeEvent({ row }) {
          this.inputMdlright = row;
          this.Delmdlname = row.mdlname;
          this.invalidflag = row.invalid.length > 0;
          this.tempRowIndex = this.$refs.xTable.getRowIndex(row);
          if (this.curtabindex == 1) {
              this.ShowContain();
          }
          if (this.curtabindex == 2) {
              this.ShowUnit();
          }
      },
      getButtonWidth() {
          if (this.$refs.buttondiv) {
              this.buttonwidth = this.$refs.buttondiv.offsetWidth - 200 + "px";
          }
      },
      rowClassName({ row, rowIndex }) {
          if (this.NewArr.indexOf(row.inputmdlid + "") > -1) {
              return "Add";
          }
          if (this.UpdateArr.indexOf(row.inputmdlid + "") > -1) {
              return "Update";
          }
          if (row.invalid.length > 0) {
              return "rm-alert";
          }
      },
      CotainClassName({ row, rowIndex }) {
          if (row.invalid.length > 0) {
              return "rm-alert";
          }
      },
      OpenDialog(flag) {
          this.actionflag = flag;
          if (flag == 1) {
              this.inputMdl = { hospcode: this.$tool.getSession(this.$tool.selectedhis), inputcode: "", mdlname: "", specimen_code: "", description: "",txtinfo1: "", txtinfo2: "",lab_advice: ""};
              this.tableDataContainEdit = [];
              this.dialogtitle = "新增批输入模板";
              this.dialogif = true;
              this.dialogFormVisible = true;
              this.CotainAddNum = 0;
              this.$nextTick(() => {
                  this.$refs.inputmdlname.$refs.input.focus();
              });
          }
          if (flag == 2) {
              if (this.inputMdlright && this.inputMdlright.inputmdlid && this.inputMdlright.inputmdlid != 0) {
                  if (this.$ddldata.IsAreaMode && this.$tool.getSession(this.$tool.loginutype) != "G" && this.inputMdlright.hospcode == "All") {
                      this.$confirm("只有管理员才能修改所有医院数据!", "提示", {
                          confirmButtonText: "确定",
                          cancelButtonText: "取消",
                          focusCancelButton: true,
                          closeOnClickModal: false,
                          type: "warning"
                      });
                  } else {
                      this.FindmdlAndCotain();
                      this.dialogtitle = "修改批输入模板";
                  }
              } else {
                  this.$message("请先选择一条需要修改的数据");
              }
          }
          if (flag == 3) {
              if (this.inputMdlright && this.inputMdlright.inputmdlid && this.inputMdlright.inputmdlid != 0) {
                  this.dialogVisible = true;
              } else {
                  this.$message("请先选择一条需要删除的数据");
              }
          }
          this.DelIndex = -1;
      },
      CancelTips() {
          if (this.actionflag < 3) {
              this.dialogif = false;
              this.dialogFormVisible = false;
              if (!this.commitflag) {
                  for (var key in this.showtempMdl) {
                      this.inputMdl[key] = this.showtempMdl[key];
                  }
              }
              this.CotainAddNum = 0;
          } else {
              this.dialogVisible = false;
          }
      },
      CommitTips() {
          this.commitflag = true;
          if (this.actionflag < 3) {
              if (this.inputMdl.mdlname == "") {
                  this.$refs.inputmdlname.focus();
                  this.$message("模板名称不能为空！");
                  return false;
              }
              if (this.isAreaMode && this.inputMdl.hospcode == "") {
                  this.$refs.inputhosp.focus();
                  this.$message("所属医院不能为空！");
                  return false;
              }
              if (this.inputMdl.inputcode.length > 20) {
                  this.$alert("拼音码字符超过20位!请手动截取后再保存!", "提示");
                  return;
              }
          } else {
          }
          this.SaveRecord();
      },
      FindmdlAndCotain() {
          this.loadingContainEdit = true;
          var mdlid = 0;
          if (this.inputMdlright.inputmdlid) mdlid = this.inputMdlright.inputmdlid;
          else mdlid = 0;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "queryforupdate",
                      inputmdlid: mdlid
                  }
              })
              .then(res => {
                  var upmdl = res.Data1[0];
                  this.tempinutMdl = JSON.parse(JSON.stringify(res.Data1[0]));
                  this.inputMdl = upmdl;
                  this.inputMdl.invalid = this.inputMdl.invalid == "1" ? true : false;
                  this.tableDataContainEdit = res.Data2;
                  this.CotainAddNum = res.Data2.length;
                  this.loadingContainEdit = false;
                  this.dialogif = true;
                  this.dialogFormVisible = true;
                  this.tempRowIndex = this.$refs.xTable.getRowIndex(this.inputMdlright);
                  this.$nextTick(() => {
                      this.$refs.inputmdlname.$refs.input.focus();
                  });
              })
              .catch(err => {
                  this.loadingContainEdit = false;
              });
      },
      ShowContain() {
          var mdlid = 0;
          if (this.inputMdlright.inputmdlid) mdlid = this.inputMdlright.inputmdlid;
          else mdlid = 0;
          this.loadingContain = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "querydetail",
                      inputmdlid: mdlid,
                      datatype: 1
                  }
              })
              .then(res => {
                  this.tableColumnContain = res.Cols; //this.ColAchieve(res.Cols);
                  var cotaindata = res.Data;
                  this.tableDataContain = cotaindata;
                  this.loadingContain = false;
              })
              .catch(err => {
                  this.loadingContain = false;
              });
      },
      ShowUnit() {
          this.loadingUnit = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "querydetail",
                      inputmdlid: this.inputMdlright.inputmdlid,
                      datatype: 2
                  }
              })
              .then(res => {
                  this.tableColumnUnit = res.Cols; //this.ColAchieve(res.Cols);
                  var dataunit = res.Data;
                  this.tableDataUnit = dataunit;
                  this.loadingUnit = false;
              })
              .catch(err => {
                  this.loadingUnit = false;
              });
      },
      handleTabClick(tab, event) {
          if (tab.label == "包含项目") {
              this.curtabindex = 1;
              this.ShowContain();
          }
          if (tab.label == "报告单元使用情况") {
              this.curtabindex = 2;
              this.ShowUnit();
          }
      },
      findList() {
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "query"
                  }
              })
              .then(res => {
                  if (res.RData.Data.length) {
                      this.isFirstLoading = true;
                      this.isAreaMode = res.isAreaMde;
                      this.tableColumn = res.RData.Cols; //this.ColAchieve(res.Cols);
                      var cotaindata = res.RData.Data;
                      for (let i = 0; i < cotaindata.length; i++) {
                          if (this.NewArr.indexOf(cotaindata[i].inputmdlid + "") > -1) {
                              cotaindata[i].isadd = "1";
                          } else {
                              cotaindata[i].isadd = "0";
                          }
                      }
                      cotaindata.sort((a, b) => a.isadd - b.isadd);
                      this.tableData = cotaindata;
                      this.tableTempData = cotaindata;
                      this.loading = false;
                      if (this.labgroup) this.labgroupchange(this.labgroup);
                      this.$nextTick(() => {
                          if (cotaindata && cotaindata.length > 0) {
                              let selectrow = null;
                              if (this.AddArray > 0) {
                                  let tmplst = cotaindata.filter(item => item.inputmdlid == this.AddArray);
                                  if (tmplst && tmplst.length > 0) {
                                      selectrow = tmplst[0];
                                  }
                              }
                              if (!selectrow) {
                                  selectrow = cotaindata[0];
                              }
                              if (selectrow) {
                                  this.$refs.xTable.setCurrentRow(selectrow);
                                  this.inputMdlright = selectrow;
                                  this.Delmdlname = selectrow.mdlname;
                                  this.invalidflag = this.inputMdlright.invalid.length > 0;
                                  this.$refs.xTable.scrollToRow(selectrow);
                                  if (this.curtabindex == 1) {
                                      this.ShowContain();
                                  }
                                  if (this.curtabindex == 2) {
                                      this.ShowUnit();
                                  }
                              }
                          }
                      });
                  } else {
                      this.tableData = [];
                      this.loading = false;
                      this.isAreaMode = res.isAreaMde;
                  }
              })
              .catch(err => {
                  this.loading = false;
              });
          this.findflag = true;
      },
      //保存数据
      SaveRecord() {
          if (this.actionflag < 3) {
              var record = this.$refs.xTable3.tableData;
              record = record.sort((a, b) => a.seq - b.seq);
              for (let i = 0, len = record.length; i < len; i++) {
                  record[i].seq = (i + 1) * 10;
              }
              var records = this.$refs.xTable3.getRecordset();
              if (records.insertRecords.length > 0) records.insertRecords = this.SetInvald(records.insertRecords);
              if (records.removeRecords.length > 0) records.removeRecords = this.SetInvald(records.removeRecords);
              if (records.updateRecords.length > 0) records.updateRecords = this.SetInvaldUp(records.updateRecords);
              var savedata = JSON.parse(JSON.stringify(this.inputMdl));
          } else {
              var savedata = JSON.parse(JSON.stringify(this.inputMdlright));
          }

          savedata.invalid = savedata.invalid ? "1" : "0";
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS112",
                  method: "post",
                  data: {
                      action: "save",
                      utype: this.actionflag,
                      saveinfo: savedata,
                      urec: records,
                      oldinfo: this.tempinutMdl
                  }
              })
              .then(res => {
                  if (res.errcode == 1) {
                      if (this.actionflag < 3) {
                          this.$message({
                              message: "保存成功",
                              type: "success"
                          });
                          this.AddArray = res.keyid + "";
                          if (this.actionflag == 1) this.NewArr.push(res.keyid + "");
                          else if (this.actionflag == 2) this.UpdateArr.push(res.keyid + "");
                          this.dialogFormVisible = false;
                          this.findList();
                      } else {
                          this.$message({
                              message: "删除成功",
                              type: "success"
                          });
                          var delrow = this.$refs.xTable.getCurrentRow();
                          if (delrow) {
                              var alld = this.$refs.xTable.tableData;
                              var rowindex = alld.indexOf(delrow);
                              this.$refs.xTable.remove(delrow);
                              var allrowindex = this.tableData.indexOf(delrow);
                              this.tableData.splice(allrowindex, 1);
                              let havedata = this.$refs.xTable.tableData;
                              if (havedata.length > 0) {
                                  if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                                  let selectrow = havedata[rowindex];
                                  this.$refs.xTable.setCurrentRow(selectrow);
                                  this.inputMdlright = selectrow;
                                  this.Delmdlname = selectrow.mdlname;
                              }
                          } else {
                              this.inputMdlright = {};
                          }
                          this.dialogVisible = false;
                      }
                  } else {
                      this.$message.error("保存失败:" + res.errmsg);
                  }
              })
              .catch(error => {
                  console.log(error);
              });
      },
      //添加一条记录
      AddRecord() {
          this.CotainAddNum = this.CotainAddNum + 1;
          let row = {
              rpt_itemcode: "",
              defaultval: "",
              defaultresult1: "",
              defaultresult2: "",
              defaultresult3: "",
              defaultresult_txt: "",
              invalid: false,
              seq: this.CotainAddNum * 10
          };
          this.$refs.xTable3.insert(row);
          var curow = this.$refs.xTable3.getTableData().fullData[this.$refs.xTable3.getTableData().fullData.length - 1];
          this.$refs.xTable3.setCurrentRow(curow);
          this.$nextTick(() => {
              var nowtablelength = this.$refs.xTable3.tableData.length;
              var nownum = (nowtablelength - 1) * 2;
              // this.$refs.xTable3.$refs.tableBody.$children[nownum].$refs.rptitemname.focus();
          });
      },
      DelRecord() {
          // var delrow = this.$refs.xTable3.getCurrentRow();
          // if (this.DelIndex < 0) this.DelIndex = this.$refs.xTable3.getRowIndex(delrow);
          // this.$refs.xTable3.remove(delrow);
          // if (this.DelIndex > -1 && this.$refs.xTable3.tableData.length > 0) {
          //     if (this.$refs.xTable3.getTableData().fullData.length > this.DelIndex) this.DelIndex = this.DelIndex;
          //     else this.DelIndex = this.DelIndex - 1;
          //     var selectrow = this.$refs.xTable3.getTableData().fullData[this.DelIndex];

          //     this.$nextTick(() => {
          //         this.$refs.xTable3.setCurrentRow(selectrow);
          //         var nownum = this.DelIndex * 2;
          //         this.$refs.xTable3.$refs.tableBody.$children[nownum].$refs.ddlrptitemSelect.focus();
          //     });
          // } else {
          //     var nowdata = this.$refs.xTable3.getTableData().fullData.filter(item => {
          //         return item._XID < delrow._XID;
          //     });
          //     if (nowdata.length > 0) {
          //         var selectrow = nowdata[nowdata.length - 1];
          //         this.$nextTick(() => {
          //             this.$refs.xTable3.setCurrentRow(selectrow);
          //             var nownum = (nowdata.length - 1) * 2;
          //             this.$refs.xTable3.$refs.tableBody.$children[nownum].$refs.ddlrptitemSelect.focus();
          //         });
          //     }
          // }
          this.$table.DelRecordSelect(this.$refs.xTable3, this.DelIndex, this);
      },
      SetInvald(records) {
          for (let i = 0, len = records.length; i < len; i++) {
              records[i].invalid = records[i].invalid ? 1 : 0;
          }
          return records;
      },
      SetInvaldUp(records) {
          for (let i = 0, len = records.length; i < len; i++) {
              records[i].cdata.invalid = records[i].cdata.invalid ? 1 : 0;
              records[i].odata.invalid = records[i].odata.invalid ? 1 : 0;
          }
          return records;
      },
      ExportCsv() {
          // this.$refs.xTable.exportCsv();
          if (typeof jsObj != "undefined") {
              let p = { prgno: "LIS112" };
              this.$pub.commexport(this, p, "批输入模板" + `.dat`);
          } else {
              this.$alert("请使用" + this.$pub.clientname + "打开系统", "错误", {
                  confirmButtonText: "确定"
              });
          }
          //window.open(this.$tool.baseUrl.replace(/api/, "z") + "filedownload?file=批输入模板&action=LIS112");
      },

      RptChangeEvent(code, row, rowIndex) {
          //row.rpt_itemcode = code;
          //var currentrow = this.$refs.xTable3.getCurrentRow();
          //this.$refs.xTable3.setSelectCell(currentrow, "rpt_itemcode");
          this.rptfocusflag = true;
      },
      specChange(code, name) {
          this.$nextTick(_ => {
              this.$refs.inputspec.$refs.ddlmainSelect.focus();
          });
      },
      hospChange(code, name) {
          this.$nextTick(_ => {
              this.$refs.inputhosp.$refs.ddlmainSelect.focus();
          });
      },
      labgroupchange(code, name) {
          this.labgroup = code;
          let totaldata = this.tableTempData;
          if (code)
              this.tableData = totaldata.filter(item => {
                  return item.labgroupid == code;
              });
          else this.tableData = totaldata;
          if (this.tableData.length > 0) {
              if (!this.tableData.find(item => item.inputcode == this.inputMdlright.inputcode)) {
                  this.Delmdlname = this.tableData[0].mdlname;
                  this.inputMdlright = this.tableData[0];
                  this.$refs.xTable.setCurrentRow(this.tableData[0]);
              }
          } else {
              this.inputMdlright = {};
              this.Delmdlname = "";
          }
      },
      itemcalsschange(code, name) {
          this.nowitemclass = code;
          this.$nextTick(_ => {
              this.$refs.inputitemclass.$refs.ddlrptitemclassSelect.focus();
          });
      },
      dialabgroupchange(code, name) {
          this.$nextTick(_ => {
              this.$refs.inputlabgroup.$refs.ddlmainSelect.focus();
          });
      },
      mdlnameChange(value) {
          var pinyinres = this.$pinyin(value, {
              style: this.$pinyin.STYLE_FIRST_LETTER, // 首字母
              heteronym: true
          });
          var inputcode = "";
          for (var i = 0, len = pinyinres.length; i < len; i++) {
              if (pinyinres[i].length > 1) {
                  pinyinres[i].sort();
              }
              inputcode += pinyinres[i][0];
          }
          this.inputMdl.inputcode = XEUtils.clone(inputcode.toLocaleUpperCase(), true);
          if (this.inputMdl.inputcode.length > 20) this.inputMdl.inputcode = this.inputMdl.inputcode.substring(0, 19);
      },
      autofocusEvent(event, element, row) {
          if (element == "mdlname") {
              this.$refs.inputspec.$refs.ddlmainSelect.focus();
          }
          if (element == "spec") {
              this.$refs.inputhosp.$refs.ddlmainSelect.focus();
          }
          if (element == "hosp") {
              this.$refs.inputhosp.$refs.ddlmainSelect.blur();
              this.$refs.inputincode.focus();
          }
          if (element == "incode") {
              this.$refs.inputitemclass.$refs.ddlrptitemclassSelect.focus();
          }
          if (element == "itemclass") {
              this.$refs.inputitemclass.$refs.ddlrptitemclassSelect.blur();
              this.$refs.inputlabgroup.$refs.ddlmainSelect.focus();
          }
          if (element == "labgroup") {
              this.$refs.inputlabgroup.$refs.ddlmainSelect.blur();
              this.$refs.inputdesc.focus();
          }
          if (element == "rptitem") {
              if (this.rptfocusflag) {
                  this.$refs.xTable3.setActiveCell(row, "defaultval");
                  this.rptfocusflag = false;
              }
          }
      },
      containeditclose({ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex, cell }) {
          var nowtablelength = this.$refs.xTable3.tableData.length;
          if (column.property == "defaultresult_txt") {
              console.log(rowIndex);
              var thisrow = this.$refs.xTable3.tableData[rowIndex + 1];
              if (!thisrow.seq) thisrow.seq = this.$refs.xTable3.tableData.length * 10;
              if (thisrow._XID) {
                  this.$nextTick(() => {
                      this.$refs.xTable3.setCurrentRow(thisrow);
                      this.$refs.xTable3.clearSelected();
                      this.$refs.xTable3.clearActived();
                      this.$nextTick(() => {
                          this.$refs.xTable3.$refs.tableBody.$children[(rowIndex + 1) * 2].$refs.rptitemname.focus();
                      });
                  });
              }
          }
          if (column.property == "seq") {
              this.$refs.xTable3.$refs.tableBody.$children[rowIndex * 2 + 1].$el.focus();
          }
      },
      rptfocus() {
          this.rptfocusflag = true;
      },
      ImportCsv() {
          this.$rm_uploadFiles({
              type: "文件上传",
              prgname: "LIS112",
              btn: {
                  confirmBtnText: "立即上传",
                  cancelBtnText: "取消"
              }
          })
              .then(res => {
                  if (res.flag == "1") {
                      this.$notify.success({
                          title: "成功",
                          message: `文件上传成功`
                      });
                      this.findList();
                  } else {
                      this.$notify.error({
                          title: "错误",
                          message: `文件上传失败`
                      });
                  }
              })
              .catch(e => {
                  console.log("error", e);
              });
      },
      handleExceed() {},
      handleBeforeUpload() {},
      handlePictureCardPreview() {},
      handleRemove() {}
  }
};
</script>

<style scoped>
::v-deep .Update {
  color: #5050fa;
}
::v-deep .Add {
  color: #b47c7c;
}
::v-deep .vxe-input--wrapper .vxe-input {
  height: auto;
  width: 200px;
  border-radius: 0px;
}
::v-deep .el-button--default.btn-custom-cancel {
  float: right !important;
  margin-left: 10px;
}

.pagetop {
  left: 0;
  right: 0;
  height: 36px;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  right: 485px;
  left: 0;
}

.middle-right {
  background-color: chartreuse;
  position: absolute;
  top: 0;
  bottom: 2px;
  overflow: visible;
  width: 480px;
  right: 0px;
}

table tr td:nth-child(odd) {
  text-align: right;
  white-space: nowrap;
}
table tr td:nth-child(even) {
  text-align: left;
}
table tr td:nth-child(1) {
  width: 80px;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
::v-deep .el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
::v-deep .el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
::v-deep  .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
</style>
